package com.huidz.calendar.activity;

import android.content.Intent;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.transition.Slide;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.huidz.calendar.R;
import com.huidz.calendar.activityadapter.MusicAdapter;
import com.huidz.calendar.util.Apiutils;

import java.util.ArrayList;
import java.util.List;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView home_list;
    private List<Fragment> fraglist;
    private ImageView comeback;
    private ArrayList<Ringtone> new_list_view = new ArrayList<>();
    private ArrayList<String> new_list_views = new ArrayList<>();
    private RelativeLayout ok_rela;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.activity_musiclist);
        inti();
    }

    public MusicAdapter myAdapter;
    public RingtoneManager ringtoneManager;

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        ok_rela = findViewById(R.id.ok_rela);
        ok_rela.setOnClickListener(this);
        home_list = findViewById(R.id.music_list);
        ringtoneManager = new RingtoneManager(this); // 铃声管理器
        ringtoneManager.setType(2);
        Cursor cursor = ringtoneManager.getCursor(); //获取铃声表,根据表名取值
        int count = cursor.getCount(); //获取铃声列表数量
        for (int i = 0; i < count; i++) {
            new_list_view.add(ringtoneManager.getRingtone(i));
            new_list_views.add("0");
        }
        home_list.setOnItemClickListener(this);
        myAdapter = new MusicAdapter(new_list_view,
                MusicActivity.this, new_list_views);
        home_list.setAdapter(myAdapter);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                for (int s = 0; s < new_list_views.size(); s++) {
                    if (new_list_view.get(s).isPlaying())
                        new_list_view.get(s).stop();
                }
                onBackPressed();
                break;
            case R.id.ok_rela:
                if (url == null) {
                    Apiutils.SetToast(MusicActivity.this, "请选择铃声");
                } else {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (!Settings.System.canWrite(this)) {
                            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                                    Uri.parse("package:" + getPackageName()));
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            getApplicationContext().startActivity(intent);
                        } else {
                            //有了权限，你要做什么呢？具体的动作
                            RingtoneManager.setActualDefaultRingtoneUri(this,
                                    RingtoneManager.TYPE_NOTIFICATION, url);
                            onBackPressed();
                        }
                    }

                }
                break;
        }
    }

    Uri url = null;
    public static String music_name="";
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (new_list_views.get(position).equals("0")) {
            for (int s = 0; s < new_list_views.size(); s++) {
                if (new_list_view.get(s).isPlaying())
                    new_list_view.get(s).stop();
                if (s != position) {
                    new_list_views.set(s, "0");
                }
            }
            new_list_views.set(position, "1");
            myAdapter.notifyDataSetChanged();
        } else {
            new_list_views.set(position, "0");
            myAdapter.notifyDataSetChanged();
        }
        if (!new_list_view.get(position).isPlaying()) {//不在播放状态
            new_list_view.get(position).play();
        } else {
            new_list_view.get(position).stop();
        }
        url = ringtoneManager.getRingtoneUri(position);
        music_name=ringtoneManager.getRingtone(position).getTitle(MusicActivity.this);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        for (int s = 0; s < new_list_views.size(); s++) {
            if (new_list_view.get(s).isPlaying())
                new_list_view.get(s).stop();
        }
        return super.onKeyUp(keyCode, event);
    }
}